#include "Subtab.h"
#include "Tab.h"
namespace NBGUI
{
	Subtab::Subtab(GUIDrawer* drawer, const char* name, const char* eleText, int _id, int eleType, Rect& _rect ):
ElementNode(drawer, name,eleText,eleType,_rect),state(Uncheck),Grid3x3(2,2,64,64),id(_id),highLight(0)
{
	//this is the uc of the tex in default png
	uv[0] = vec2f(0,0);
	uv[1] = vec2f(0,64.0f/512);
	uv[2] = vec2f(64.0f/512,64.0f/512);
	uv[3] = vec2f(64.0f/512,0);

	uv[4] = vec2f(64.0f/512,0);
	uv[5] = vec2f(64.0f/512,64.0f/512);
	uv[6] = vec2f(128.0f/512,64.0f/512);
	uv[7] = vec2f(128.0f/512,0);

	textDrawingPack = drawer->font->addText(elementText.c_str(),(float)_rect.x+5,(float)_rect.y + _rect.h/2,6);

	info i;
	i.i = id;
	infoList.push_back(i);

}

Subtab::Subtab( GUIDrawer* drawer, const char* name, const char* eleText, int _id, vec2f* _uv, int eleType /*= NONE*/, Rect& _rect /*= Rect()*/ ):
ElementNode(drawer, name,eleText,eleType,_rect),state(Uncheck),Grid3x3(2,2,64,64),id(_id),highLight(0)
{
	memcpy_s(uv,sizeof(vec2f)*4,_uv,sizeof(vec2f)*4);
}

Subtab::~Subtab()
{
}

void Subtab::draw()
{
	if(hide)
		return;
	//draw something based on the element type
	//draw the children
	//need to load texture to draw
	//need to know the x y and size of the element
	pDrawer->pRenderer->applyTexture(0);
	pDrawer->pRenderer->drawIndexed(startIndex,indexLen,pDrawer->vboId,pDrawer->iboId);
	
	if(highLight == 0)
	{
		pDrawer->pRenderer->drawIndexed(startIndex,indexLen/2,pDrawer->vboId,pDrawer->iboId);
	}
	else if(highLight == 1)
	{
		pDrawer->pRenderer->drawIndexed(startIndex,indexLen,pDrawer->vboId,pDrawer->iboId);
	}
	
	pDrawer->font->drawStrPack(textDrawingPack);

	 
	for(auto it = children.begin(); it != children.end(); ++it)
	{
		if(id == static_cast<Tab*>(parent)->getCurrentId())
			(*it)->draw();
	}
}

void Subtab::buildVI( std::vector<Vertex>* vertices, std::vector<unsigned int>* indices,unsigned int screenWidth, unsigned int screenHeight )
{
	startIndex = indices->size();
	for (int checkValue = 0; checkValue< 2; ++checkValue)
	{//for highlight
	for (int i = 0; i < 9; ++i)
	{
		Rect gridRect = grid[i];
		float left, right, top, bottom;



		if(i == TOPRIGHT || i == MIDRIGHT || i == BOTTOMRIGHT)
		{
			left = (-(float)screenWidth / 2) + (float)rect.x + rect.w - edgeWidth;
		}
		else
		{
			left = (-(float)screenWidth / 2) + (float)rect.x + (float)gridRect.x ;
		}



		if(i == TOPMID || i == MIDMID || i == BOTTOMMID)
		{
			//mid grid , draw as the xml says
			right = left + rect.w - 2 * edgeWidth;
		}
		else
		{
			right = left + (float)gridRect.w ;
		}

		if (i > MIDRIGHT && i <= BOTTOMRIGHT)
		{
			//bottom 3 grid
			top = (float)screenHeight / 2 - (float)rect.y - (rect.h - edgeHeight);
		}
		else
		{
			top = (float)screenHeight / 2 - (float)rect.y -  (float)gridRect.y;
		}

		if(i > TOPRIGHT && i <= MIDRIGHT)
		{
			bottom = top - (rect.h - 2 * edgeHeight) ;
		}
		else
		{
			bottom = top - (float)gridRect.h ;
		}



		Vertex eleVertices[4];
		unsigned int currentVertexNum = vertices->size();
		unsigned int eleIndices[6]={0,1,2,0,2,3};


		eleVertices[0].pos = vec3f(left, top, 0.0f);  // Top left.
		eleVertices[0].coord = uv[4*checkValue];
		eleVertices[0].coord.x += gridRect.x/512.0f;
		eleVertices[0].coord.y += gridRect.y/512.0f;

		eleVertices[1].pos = vec3f(left, bottom, 0.0f);  // Bottom left.
		eleVertices[1].coord = eleVertices[0].coord;
		eleVertices[1].coord.y += gridRect.h/512.0f;

		eleVertices[2].pos = vec3f(right, bottom, 0.0f);  // Bottom right.
		eleVertices[2].coord = eleVertices[0].coord;
		eleVertices[2].coord.x += gridRect.w/512.0f;
		eleVertices[2].coord.y += gridRect.h/512.0f;

		eleVertices[3].pos = vec3f(right, top, 0.0f);  // Top right.
		eleVertices[3].coord = eleVertices[0].coord;
		eleVertices[3].coord.x += gridRect.w/512.0f;

		for (int i = 0; i < 4; ++i)
		{
			vertices->push_back(eleVertices[i]);
		}



		for (int i = 0; i  < 6; ++i)
		{
			indices->push_back(currentVertexNum + eleIndices[i]);
		}

		indexLen += 6;
	}
	}
}

void Subtab::handleEvent( Event* e )
{
	if(id == static_cast<Tab*>(parent)->getCurrentId())
		ElementNode::handleEvent( e );

	switch(e->type)
	{
	case MOUSE_MOVE:
		if(isInside(e))
		{
			highLight = 1;
		}
		else
		{
			highLight = 0;
		}
		break;
	case MOUSE_BUTTON_RELEASE:
		if( !isInside(e) )
		{
			return;
		}

		//swith to current page 
		parent->doSomething( infoList,std::string("SwitchTab"));
		 
	}
}

}